<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【NodeJS】NodeJS入门学习 | Vixcity</title><meta name="keywords" content="Node"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="NodeJS学习内容项目相关12345678# 初始化项目npm init -y# 安装依赖npm i# 启动项目node app.js  依赖 https:&#x2F;&#x2F;www.lodashjs.com&#x2F; https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;chokidar  基础知识123&#x2F;&#x2F; globalThis 定义的对象可以在全局中被访问到&#x2F;&#x2F; 例：globalThis.a &#x3D; 2  No">
<meta property="og:type" content="article">
<meta property="og:title" content="【NodeJS】NodeJS入门学习">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="NodeJS学习内容项目相关12345678# 初始化项目npm init -y# 安装依赖npm i# 启动项目node app.js  依赖 https:&#x2F;&#x2F;www.lodashjs.com&#x2F; https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;chokidar  基础知识123&#x2F;&#x2F; globalThis 定义的对象可以在全局中被访问到&#x2F;&#x2F; 例：globalThis.a &#x3D; 2  No">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/NodeJs.jpg">
<meta property="article:published_time" content="2022-07-06T16:00:00.000Z">
<meta property="article:modified_time" content="2022-07-12T16:00:00.000Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="Node">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/NodeJs.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【NodeJS】NodeJS入门学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-07-13 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/NodeJs.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【NodeJS】NodeJS入门学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-07-12T16:00:00.000Z" title="更新于 2022-07-13 00:00:00">2022-07-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/Node/">Node</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>8分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【NodeJS】NodeJS入门学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="NodeJS学习内容"><a href="#NodeJS学习内容" class="headerlink" title="NodeJS学习内容"></a>NodeJS学习内容</h2><h2 id="项目相关"><a href="#项目相关" class="headerlink" title="项目相关"></a>项目相关</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 初始化项目</span></span><br><span class="line">npm init -y</span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装依赖</span></span><br><span class="line">npm i</span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动项目</span></span><br><span class="line">node app.js</span><br></pre></td></tr></table></figure>

<h2 id="依赖"><a href="#依赖" class="headerlink" title="依赖"></a>依赖</h2><ul>
<li><a target="_blank" rel="noopener" href="https://www.lodashjs.com/">https://www.lodashjs.com/</a></li>
<li><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/chokidar">https://www.npmjs.com/package/chokidar</a></li>
</ul>
<h2 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// globalThis 定义的对象可以在全局中被访问到</span></span><br><span class="line"><span class="comment">// 例：</span></span><br><span class="line">globalThis.a = <span class="number">2</span></span><br></pre></td></tr></table></figure>

<h2 id="Node文档"><a href="#Node文档" class="headerlink" title="Node文档"></a>Node文档</h2><h3 id="Buffer"><a href="#Buffer" class="headerlink" title="Buffer"></a>Buffer</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/learn/nodejs-buffers">http://nodejs.cn/learn/nodejs-buffers</a></p>
<h3 id="文件系统模块"><a href="#文件系统模块" class="headerlink" title="文件系统模块"></a>文件系统模块</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/learn/the-nodejs-fs-module">http://nodejs.cn/learn/the-nodejs-fs-module</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> chokidar = <span class="built_in">require</span>(<span class="string">&#x27;chokidar&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 读取文件</span></span><br><span class="line">fs.readFile(<span class="string">&#x27;./helloWorld.txt&#x27;</span>, <span class="string">&#x27;utf8&#x27;</span>, <span class="function">(<span class="params">err, data</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(data)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 写入文件 - 内容覆盖</span></span><br><span class="line">fs.writeFile(<span class="string">&#x27;./helloWorld.txt&#x27;</span>, <span class="string">&#x27;this is a test&#x27;</span>, <span class="string">&#x27;utf8&#x27;</span>, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;写入成功&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 追加写入，可以用buffer写，也可以用普通的文本字符写</span></span><br><span class="line"><span class="keyword">const</span> buf = Buffer.from(<span class="string">&#x27;\nHello World!&#x27;</span>)</span><br><span class="line">fs.appendFile(<span class="string">&#x27;./helloWorld.txt&#x27;</span>,buf,<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;追加写入成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取文件信息 主要用来判断 文件||文件夹</span></span><br><span class="line">fs.stat(<span class="string">&#x27;./helloWorld.txt&#x27;</span>, <span class="function">(<span class="params">err, stats</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(stats.isFile())</span><br><span class="line">    <span class="built_in">console</span>.log(stats.isDirectory())</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 重命名文件</span></span><br><span class="line">fs.rename(<span class="string">&#x27;./helloWorld.txt&#x27;</span>, <span class="string">&#x27;hello.txt&#x27;</span>, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;重命名成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除文件</span></span><br><span class="line">fs.unlink(<span class="string">&#x27;./helloWorld.txt&#x27;</span>, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;删除成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建文件夹</span></span><br><span class="line">fs.mkdir(<span class="string">&#x27;./testDir&#x27;</span>, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;创建文件夹成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 递归创建</span></span><br><span class="line">fs.mkdir(<span class="string">&#x27;./testDir/testDirSon&#x27;</span>, &#123;</span><br><span class="line">    <span class="attr">recursive</span>: <span class="literal">true</span></span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;创建文件夹成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 读取文件夹</span></span><br><span class="line">fs.readdir(<span class="string">&#x27;./&#x27;</span>, &#123;</span><br><span class="line">    <span class="comment">// 带上文件类型</span></span><br><span class="line">    <span class="attr">withFileTypes</span>: <span class="literal">true</span></span><br><span class="line">&#125;, <span class="function">(<span class="params">err, files</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(files)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除文件夹</span></span><br><span class="line">fs.rmdir(<span class="string">&#x27;./testDir&#x27;</span>, &#123;</span><br><span class="line">    <span class="comment">// 递归删除，可以删除内部有东西的文件夹</span></span><br><span class="line">    <span class="attr">recursive</span>: <span class="literal">true</span></span><br><span class="line">&#125;, <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;删除文件夹成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听文件变化</span></span><br><span class="line">fs.watch(<span class="string">&#x27;./&#x27;</span>, &#123;</span><br><span class="line">    <span class="comment">// 监视子目录</span></span><br><span class="line">    <span class="attr">recursive</span>: <span class="literal">true</span></span><br><span class="line">&#125;, <span class="function">(<span class="params">eventType, fileName</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(eventType, fileName)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听文件变化可以用另一个更好的包</span></span><br><span class="line"><span class="comment">// chokidar</span></span><br><span class="line"><span class="comment">// npm i chokidar --save-dev</span></span><br><span class="line">chokidar.watch(<span class="string">&#x27;./&#x27;</span>, &#123;</span><br><span class="line">    <span class="comment">// 不监听</span></span><br><span class="line">    <span class="attr">ignored</span>: <span class="string">&#x27;./node_modules&#x27;</span></span><br><span class="line">&#125;).on(<span class="string">&#x27;all&#x27;</span>, <span class="function">(<span class="params">event, path</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(event, path)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="文件流"><a href="#文件流" class="headerlink" title="文件流"></a>文件流</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/learn/nodejs-streams">http://nodejs.cn/learn/nodejs-streams</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 读取流</span></span><br><span class="line"><span class="keyword">let</span> rs = fs.createReadStream(<span class="string">&#x27;./app.js&#x27;</span>, &#123;</span><br><span class="line">    <span class="comment">// 每次 on data 的一个数据量</span></span><br><span class="line">    <span class="attr">highWaterMark</span>: <span class="number">100</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> count = <span class="number">1</span></span><br><span class="line">rs.on(<span class="string">&#x27;data&#x27;</span>, <span class="function"><span class="params">chunk</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(chunk.toString())</span><br><span class="line">    <span class="built_in">console</span>.log(count++)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">rs.on(<span class="string">&#x27;end&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;读取完成&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 写入流</span></span><br><span class="line"><span class="keyword">let</span> ws = fs.createWriteStream(<span class="string">&#x27;./helloWorld.txt&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">0</span></span><br><span class="line"><span class="keyword">let</span> timer = <span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (num &lt; <span class="number">10</span>) &#123;</span><br><span class="line">        ws.write(num + <span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">        num++</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        ws.end(<span class="string">&#x27;写入完成&#x27;</span>)</span><br><span class="line">        <span class="built_in">clearInterval</span>(timer)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;, <span class="number">200</span>)</span><br><span class="line"></span><br><span class="line">ws.on(<span class="string">&#x27;finish&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;写入完成&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 管道流，从数据的来源，通过管道，一段一段的流向目标</span></span><br><span class="line"><span class="keyword">let</span> rs = fs.createReadStream(<span class="string">&#x27;./app.js&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> ws = fs.createWriteStream(<span class="string">&#x27;./helloWorld.txt&#x27;</span>)</span><br><span class="line"><span class="comment">// 读取流的pipe方法，写入到写入流里面</span></span><br><span class="line">rs.pipe(ws)</span><br></pre></td></tr></table></figure>

<h3 id="path"><a href="#path" class="headerlink" title="path"></a>path</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/api/path.html">http://nodejs.cn/api/path.html</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(path.basename(<span class="string">&#x27;/app.js&#x27;</span>,<span class="string">&#x27;.js&#x27;</span>)) <span class="comment">// app.js 后面的为省略后缀名</span></span><br><span class="line"><span class="built_in">console</span>.log(path.dirname(<span class="string">&#x27;/app.js&#x27;</span>)) <span class="comment">// /</span></span><br><span class="line"><span class="built_in">console</span>.log(path.extname(<span class="string">&#x27;app.js&#x27;</span>)) <span class="comment">// .js</span></span><br><span class="line"><span class="built_in">console</span>.log(path.join(<span class="string">&#x27;/nodeJS-learning/&#x27;</span>,<span class="string">&#x27;/app.js&#x27;</span>)) <span class="comment">// 拼接 \nodeJS-learning\app.js</span></span><br><span class="line"><span class="built_in">console</span>.log(path.normalize(<span class="string">&#x27;/nodeJS-learning//app.js&#x27;</span>)) <span class="comment">// 规范化路径 \nodeJS-learning\app.js</span></span><br><span class="line"><span class="built_in">console</span>.log(path.resolve(<span class="string">&#x27;./app.js&#x27;</span>)) <span class="comment">// 绝对路径 C:\Users\47072\Desktop\project\nodeJS-learning\app.js</span></span><br><span class="line"><span class="keyword">let</span> pathObj = path.parse(<span class="string">&quot;/nodeJS-learning/app.js&quot;</span>) <span class="comment">// 将路径转化为对象</span></span><br><span class="line"><span class="built_in">console</span>.log(pathObj) </span><br><span class="line"><span class="built_in">console</span>.log(path.format(pathObj)) <span class="comment">// 将对象转化为路径</span></span><br><span class="line"><span class="built_in">console</span>.log(path.sep) <span class="comment">// 系统路径的分隔符</span></span><br><span class="line"><span class="built_in">console</span>.log(path.win32.sep) <span class="comment">// window下的分隔符</span></span><br><span class="line"><span class="built_in">console</span>.log(__filename) <span class="comment">// 当前所在路径绝对目录 是绝对正确的 resolve 执行的地方不同可能是会有错误的</span></span><br><span class="line"><span class="built_in">console</span>.log(__dirname) <span class="comment">// 当前目录绝对目录</span></span><br></pre></td></tr></table></figure>

<h3 id="事件触发器"><a href="#事件触发器" class="headerlink" title="事件触发器"></a>事件触发器</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/api/events.html">http://nodejs.cn/api/events.html</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">&#x27;events&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyEmitter</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> myEmitter = <span class="keyword">new</span> MyEmitter()</span><br><span class="line"></span><br><span class="line">myEmitter.on(<span class="string">&#x27;hi&#x27;</span>, <span class="function">(<span class="params">a,b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;触发了事件&#x27;</span>,a+b)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">myEmitter.once(<span class="string">&#x27;once&#x27;</span>, <span class="function">(<span class="params">a,b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;触发了一次&#x27;</span>,a+b)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 触发事件</span></span><br><span class="line">myEmitter.emit(<span class="string">&#x27;hi&#x27;</span>,<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 只会触发一次</span></span><br><span class="line">myEmitter.emit(<span class="string">&#x27;once&#x27;</span>,<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line">myEmitter.emit(<span class="string">&#x27;once&#x27;</span>,<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> fn1 = <span class="function">(<span class="params">a,b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;带参&#x27;</span>,a+b)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> fn2 = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;不带参&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">myEmitter.on(<span class="string">&#x27;hi1&#x27;</span>, fn1)</span><br><span class="line">myEmitter.on(<span class="string">&#x27;hi1&#x27;</span>, fn2)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 移除数组</span></span><br><span class="line"><span class="comment">// myEmitter.removeListener(&#x27;hi1&#x27;,fn1)</span></span><br><span class="line"><span class="comment">// 移除全部数组</span></span><br><span class="line">myEmitter.removeAllListeners(<span class="string">&#x27;hi1&#x27;</span>)</span><br><span class="line">myEmitter.emit(<span class="string">&#x27;hi1&#x27;</span>,<span class="number">1</span>,<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="util"><a href="#util" class="headerlink" title="util"></a>util</h3><p><a target="_blank" rel="noopener" href="http://nodejs.cn/api/util.html">http://nodejs.cn/api/util.html</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> util = <span class="built_in">require</span>(<span class="string">&#x27;util&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">hello</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;hello World!&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将异步函数转换成为有回调风格的函数</span></span><br><span class="line"><span class="keyword">let</span> helloCallBack = util.callbackify(hello)</span><br><span class="line"></span><br><span class="line">helloCallBack(<span class="function">(<span class="params">err, res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 转换成为promise版本的函数</span></span><br><span class="line"><span class="keyword">let</span> stat = util.promisify(fs.stat)</span><br><span class="line"><span class="keyword">let</span> stat1 = util.promisify(fs.stat)</span><br><span class="line"></span><br><span class="line">stat(<span class="string">&#x27;./app.js&#x27;</span>).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">&#125;).catch(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">statFn</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> stats = <span class="keyword">await</span> stat1(<span class="string">&#x27;./app.js&#x27;</span>)</span><br><span class="line">        <span class="built_in">console</span>.log(stats)</span><br><span class="line">    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(e)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">statFn()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 判断值是否为date类型</span></span><br><span class="line"><span class="built_in">console</span>.log(util.types.isDate(<span class="keyword">new</span> <span class="built_in">Date</span>()), util.types.isDate(<span class="number">1</span>))</span><br></pre></td></tr></table></figure>

<h3 id="http基本知识"><a href="#http基本知识" class="headerlink" title="http基本知识"></a>http基本知识</h3><hr>
<ul>
<li>GET 请求指定的⻚⾯信息，并返回实体主体</li>
<li>HEAD 类似于get请求，只不过返回的响应中没有具体的内容，⽤于获取报头</li>
<li>POST 向指定资源提交数据进⾏处理请求。数据被包含在请求体中。</li>
<li>PUT 从客户端向服务器传送的数据取代指定的⽂档的内容</li>
<li>DELETE 请求服务器删除指定的⻚⾯</li>
<li>CONNECT HTTP/1.1协议中预留给能够将连接改为管道⽅式的代理服务器。</li>
<li>OPTIONS 允许客户端查看服务器的性能</li>
<li>TRACE 回显服务器收到的请求，主要⽤于测试或诊断</li>
</ul>
<hr>
<table>
<thead>
<tr>
<th align="left">应答头</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Allow</td>
<td align="left">服务器⽀持哪些请求⽅法（如get、post等）。</td>
</tr>
<tr>
<td align="left">Content-Encoding</td>
<td align="left">⽂档的编码⽅法。只有在解码之后才可以得到Content-Type头指定的内容类型。利⽤gzip压缩能减少HTML⽂档的下载时间。</td>
</tr>
<tr>
<td align="left">Content-Length</td>
<td align="left">表示内容⻓度。只有当浏览器使⽤持久http连接时才需要这个数据。</td>
</tr>
<tr>
<td align="left">Content-Type</td>
<td align="left">表示⽂档属于什么MIME(文件)类型。</td>
</tr>
<tr>
<td align="left">Date</td>
<td align="left">当前的GMT时间。</td>
</tr>
<tr>
<td align="left">Expires</td>
<td align="left">资源什么时候过期，不再缓存，会重新向服务器请求页面。</td>
</tr>
<tr>
<td align="left">Last-Modified</td>
<td align="left">⽂档最后改动时间。</td>
</tr>
<tr>
<td align="left">Location</td>
<td align="left">重定向的地址。</td>
</tr>
<tr>
<td align="left">Server</td>
<td align="left">服务器的名字。</td>
</tr>
<tr>
<td align="left">Set-Cookie</td>
<td align="left">设置和⻚⾯关联的Cookie。</td>
</tr>
<tr>
<td align="left">WWW-Authenticate</td>
<td align="left">定义了使⽤何种验证⽅式去获取对资源的链接。</td>
</tr>
</tbody></table>
<hr>
<h4 id="常⻅的http状态码"><a href="#常⻅的http状态码" class="headerlink" title="常⻅的http状态码"></a>常⻅的http状态码</h4><ul>
<li>200 请求成功</li>
<li>301 资源被永久转移到其他URL</li>
<li>404 请求的资源（⽹⻚等）不存在</li>
<li>500 内部服务器错误</li>
</ul>
<hr>
<table>
<thead>
<tr>
<th align="left">分类</th>
<th>分类描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1**</td>
<td>信息，服务器收到请求，需要请求者继续执⾏操作</td>
</tr>
<tr>
<td align="left">2**</td>
<td>成功，操作被成功接收并处理</td>
</tr>
<tr>
<td align="left">3**</td>
<td>重定向，需要进⼀步的操作以完成请求</td>
</tr>
<tr>
<td align="left">4**</td>
<td>客户端错误，请求包含语法错误或⽆法完成请求</td>
</tr>
<tr>
<td align="left">5**</td>
<td>服务器错误，服务器在处理请求的过程中发⽣了错误</td>
</tr>
</tbody></table>
<hr>
<h4 id="Content-Type-内容类型"><a href="#Content-Type-内容类型" class="headerlink" title="Content-Type 内容类型"></a>Content-Type 内容类型</h4><p>常⻅的媒体格式类型如下</p>
<ul>
<li>text/html:HTML格式</li>
<li>text/plain:纯⽂本格式</li>
<li>text/xml:XML格式</li>
<li>image/gif:gif图⽚格式</li>
<li>image/jpeg:jpg图⽚格式</li>
<li>image/png:png图⽚格式</li>
<li>multipart/form-data:需要在表单中进⾏⽂件上传时，就需要使⽤该格式</li>
</ul>
<p>以application开头的媒体格式类型：</p>
<ul>
<li>application/xhtml+xml:XHTML格式</li>
<li>application/xml:XML数据格式</li>
<li>application/atom+xml:Atom XML聚合格式</li>
<li>application/json:JSON数据格式</li>
<li>application/pdf:pdf格式</li>
<li>application/msword:Word⽂档格式</li>
<li>application/octet-stream:⼆进制流数据（常⻅的⽂件下载)</li>
<li>application/x-www-form-urlencoded:表单中默认的encType,表单数据被编码为key/value格式发送到服务器</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">Vixcity</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/">http://vixcity.gitee.io/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://vixcity.gitee.io/my-gitee-blog" target="_blank">Vixcity</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/Node/">Node</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/NodeJs.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/"><img class="prev-cover" src="/my-gitee-blog/img/hexo.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【Hexo】标签样式列表参考 - 3</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2022/04/25/%E3%80%90%E6%91%84%E5%BD%B1%E4%BB%8E%E9%9B%B6%E5%88%B0%E4%B8%80%E3%80%9112.%E6%8B%8D%E6%91%84%E5%A4%8D%E5%8F%A4%E9%A2%98%E6%9D%90/"><img class="next-cover" src="/my-gitee-blog/img/%E6%91%84%E5%BD%B1.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【摄影从零到一】拍摄复古题材</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-13</div><div class="title">【NodeJS】NodeJS 服务端请求</div></div></a></div><div><a href="/my-gitee-blog/2021/10/20/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91koa%E6%B4%8B%E8%91%B1%E6%A8%A1%E5%9E%8B%E4%B8%8Ekoa-compose/" title="【源码阅读】koa洋葱模型与koa-compose"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-20</div><div class="title">【源码阅读】koa洋葱模型与koa-compose</div></div></a></div><div><a href="/my-gitee-blog/2021/10/23/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91update-notifier%20%E6%9B%B4%E6%96%B0%20CLI%20%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E9%80%9A%E7%9F%A5-%E8%AF%A6%E8%A7%A3/" title="【源码阅读】update-notifier 更新 CLI 应用程序的通知-详解"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-23</div><div class="title">【源码阅读】update-notifier 更新 CLI 应用程序的通知-详解</div></div></a></div><div><a href="/my-gitee-blog/2021/10/25/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91validate-npm-package-name%20%E9%AA%8C%E8%AF%81%E6%98%AF%E5%90%A6%E6%98%AF%E4%B8%AA%E6%9C%89%E6%95%88%E7%9A%84%20npm%20%E5%8C%85%E5%90%8D/" title="【源码阅读】validate-npm-package-name 验证是否是个有效的 npm 包名"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-25</div><div class="title">【源码阅读】validate-npm-package-name 验证是否是个有效的 npm 包名</div></div></a></div><div><a href="/my-gitee-blog/2021/10/15/%E3%80%90NodeJS%E3%80%91Node.js%20+%20Vue%20%E5%AE%9E%E7%8E%B0%20Excel%20%E5%AF%BC%E5%87%BA%E4%B8%8E%E4%BF%9D%E5%AD%98/" title="【NodeJS】Node.js + Vue 实现 Excel 导出与保存"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-15</div><div class="title">【NodeJS】Node.js + Vue 实现 Excel 导出与保存</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/NodeJs.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>